home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH12 / SRC / LIGHT2.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-08  |  18.0 KB  |  582 lines

  1. VERSION 4.00
  2. Begin VB.Form LightForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Light2"
  6.    ClientHeight    =   6075
  7.    ClientLeft      =   1335
  8.    ClientTop       =   630
  9.    ClientWidth     =   6030
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   0
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   6765
  21.    KeyPreview      =   -1  'True
  22.    Left            =   1275
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   6075
  25.    ScaleWidth      =   6030
  26.    Top             =   0
  27.    Width           =   6150
  28.    Begin VB.TextBox KaText 
  29.       BeginProperty Font 
  30.          name            =   "MS Sans Serif"
  31.          charset         =   0
  32.          weight          =   700
  33.          size            =   8.25
  34.          underline       =   0   'False
  35.          italic          =   0   'False
  36.          strikethrough   =   0   'False
  37.       EndProperty
  38.       Height          =   285
  39.       Left            =   2040
  40.       TabIndex        =   10
  41.       Text            =   "0.30"
  42.       Top             =   5760
  43.       Width           =   855
  44.    End
  45.    Begin VB.TextBox KdText 
  46.       BeginProperty Font 
  47.          name            =   "MS Sans Serif"
  48.          charset         =   0
  49.          weight          =   700
  50.          size            =   8.25
  51.          underline       =   0   'False
  52.          italic          =   0   'False
  53.          strikethrough   =   0   'False
  54.       EndProperty
  55.       Height          =   285
  56.       Left            =   480
  57.       TabIndex        =   8
  58.       Text            =   "0.65"
  59.       Top             =   5760
  60.       Width           =   855
  61.    End
  62.    Begin VB.TextBox PhiText 
  63.       BeginProperty Font 
  64.          name            =   "MS Sans Serif"
  65.          charset         =   0
  66.          weight          =   700
  67.          size            =   8.25
  68.          underline       =   0   'False
  69.          italic          =   0   'False
  70.          strikethrough   =   0   'False
  71.       EndProperty
  72.       Height          =   285
  73.       Left            =   3600
  74.       TabIndex        =   6
  75.       Text            =   "0.1571"
  76.       Top             =   5400
  77.       Width           =   855
  78.    End
  79.    Begin VB.TextBox ThetaText 
  80.       BeginProperty Font 
  81.          name            =   "MS Sans Serif"
  82.          charset         =   0
  83.          weight          =   700
  84.          size            =   8.25
  85.          underline       =   0   'False
  86.          italic          =   0   'False
  87.          strikethrough   =   0   'False
  88.       EndProperty
  89.       Height          =   285
  90.       Left            =   2040
  91.       TabIndex        =   4
  92.       Text            =   "1.8850"
  93.       Top             =   5400
  94.       Width           =   855
  95.    End
  96.    Begin VB.TextBox RText 
  97.       BeginProperty Font 
  98.          name            =   "MS Sans Serif"
  99.          charset         =   0
  100.          weight          =   700
  101.          size            =   8.25
  102.          underline       =   0   'False
  103.          italic          =   0   'False
  104.          strikethrough   =   0   'False
  105.       EndProperty
  106.       Height          =   285
  107.       Left            =   480
  108.       TabIndex        =   2
  109.       Text            =   "20.0000"
  110.       Top             =   5400
  111.       Width           =   855
  112.    End
  113.    Begin VB.PictureBox Pict 
  114.       AutoRedraw      =   -1  'True
  115.       BackColor       =   &H00FFFF80&
  116.       BeginProperty Font 
  117.          name            =   "MS Sans Serif"
  118.          charset         =   0
  119.          weight          =   700
  120.          size            =   8.25
  121.          underline       =   0   'False
  122.          italic          =   0   'False
  123.          strikethrough   =   0   'False
  124.       EndProperty
  125.       Height          =   5295
  126.       Left            =   0
  127.       Picture         =   "Light2.frx":0000
  128.       ScaleHeight     =   -14
  129.       ScaleLeft       =   -7
  130.       ScaleMode       =   0  'User
  131.       ScaleTop        =   7
  132.       ScaleWidth      =   15.926
  133.       TabIndex        =   0
  134.       Top             =   0
  135.       Width           =   6015
  136.    End
  137.    Begin VB.Label Label1 
  138.       Caption         =   "k"
  139.       BeginProperty Font 
  140.          name            =   "MS Sans Serif"
  141.          charset         =   0
  142.          weight          =   700
  143.          size            =   8.25
  144.          underline       =   0   'False
  145.          italic          =   0   'False
  146.          strikethrough   =   0   'False
  147.       EndProperty
  148.       Height          =   255
  149.       Index           =   5
  150.       Left            =   1680
  151.       TabIndex        =   12
  152.       Top             =   5760
  153.       Width           =   135
  154.    End
  155.    Begin VB.Label Label1 
  156.       Caption         =   "a"
  157.       BeginProperty Font 
  158.          name            =   "MS Sans Serif"
  159.          charset         =   0
  160.          weight          =   700
  161.          size            =   8.25
  162.          underline       =   0   'False
  163.          italic          =   0   'False
  164.          strikethrough   =   0   'False
  165.       EndProperty
  166.       Height          =   255
  167.       Index           =   4
  168.       Left            =   1800
  169.       TabIndex        =   11
  170.       Top             =   5880
  171.       Width           =   135
  172.    End
  173.    Begin VB.Label Label1 
  174.       Caption         =   "d"
  175.       BeginProperty Font 
  176.          name            =   "MS Sans Serif"
  177.          charset         =   0
  178.          weight          =   700
  179.          size            =   8.25
  180.          underline       =   0   'False
  181.          italic          =   0   'False
  182.          strikethrough   =   0   'False
  183.       EndProperty
  184.       Height          =   255
  185.       Index           =   3
  186.       Left            =   240
  187.       TabIndex        =   9
  188.       Top             =   5880
  189.       Width           =   135
  190.    End
  191.    Begin MSComDlg.CommonDialog LoadDialog 
  192.       Left            =   4560
  193.       Top             =   5160
  194.       _Version        =   65536
  195.       _ExtentX        =   847
  196.       _ExtentY        =   847
  197.       _StockProps     =   0
  198.       CancelError     =   -1  'True
  199.    End
  200.    Begin VB.Label Label1 
  201.       Caption         =   "k"
  202.       BeginProperty Font 
  203.          name            =   "MS Sans Serif"
  204.          charset         =   0
  205.          weight          =   700
  206.          size            =   8.25
  207.          underline       =   0   'False
  208.          italic          =   0   'False
  209.          strikethrough   =   0   'False
  210.       EndProperty
  211.       Height          =   255
  212.       Index           =   7
  213.       Left            =   120
  214.       TabIndex        =   7
  215.       Top             =   5760
  216.       Width           =   135
  217.    End
  218.    Begin VB.Label Label1 
  219.       Caption         =   "Phi"
  220.       BeginProperty Font 
  221.          name            =   "MS Sans Serif"
  222.          charset         =   0
  223.          weight          =   700
  224.          size            =   8.25
  225.          underline       =   0   'False
  226.          italic          =   0   'False
  227.          strikethrough   =   0   'False
  228.       EndProperty
  229.       Height          =   255
  230.       Index           =   2
  231.       Left            =   3240
  232.       TabIndex        =   5
  233.       Top             =   5400
  234.       Width           =   375
  235.    End
  236.    Begin VB.Label Label1 
  237.       Caption         =   "Theta"
  238.       BeginProperty Font 
  239.          name            =   "MS Sans Serif"
  240.          charset         =   0
  241.          weight          =   700
  242.          size            =   8.25
  243.          underline       =   0   'False
  244.          italic          =   0   'False
  245.          strikethrough   =   0   'False
  246.       EndProperty
  247.       Height          =   255
  248.       Index           =   1
  249.       Left            =   1440
  250.       TabIndex        =   3
  251.       Top             =   5400
  252.       Width           =   495
  253.    End
  254.    Begin VB.Label Label1 
  255.       Caption         =   "R"
  256.       BeginProperty Font 
  257.          name            =   "MS Sans Serif"
  258.          charset         =   0
  259.          weight          =   700
  260.          size            =   8.25
  261.          underline       =   0   'False
  262.          italic          =   0   'False
  263.          strikethrough   =   0   'False
  264.       EndProperty
  265.       Height          =   255
  266.       Index           =   0
  267.       Left            =   240
  268.       TabIndex        =   1
  269.       Top             =   5400
  270.       Width           =   255
  271.    End
  272.    Begin VB.Menu mnuFile 
  273.       Caption         =   "&File"
  274.       Begin VB.Menu mnuFileLoad 
  275.          Caption         =   "&Load..."
  276.          Shortcut        =   ^L
  277.       End
  278.       Begin VB.Menu mnuFileSep 
  279.          Caption         =   "-"
  280.       End
  281.       Begin VB.Menu mnuFileExit 
  282.          Caption         =   "E&xit"
  283.       End
  284.    End
  285. Attribute VB_Name = "LightForm"
  286. Attribute VB_Creatable = False
  287. Attribute VB_Exposed = False
  288. Option Explicit
  289. Dim SysPalSize As Integer
  290. Dim NumStaticColors As Integer
  291. Dim StaticColor1 As Integer
  292. Dim StaticColor2 As Integer
  293. Dim syspal(0 To 255) As PALETTEENTRY
  294. ' Location of viewing eye.
  295. Dim EyeR As Single
  296. Dim EyeTheta As Single
  297. Dim EyePhi As Single
  298. Const Dtheta = PI / 20
  299. Const Dphi = PI / 20
  300. Const dr = 1
  301. ' Location of focus point.
  302. Const FocusX = 0#
  303. Const FocusY = 0#
  304. Const FocusZ = 0#
  305. Dim Projector(1 To 4, 1 To 4) As Single
  306. Dim ThePicture As ObjPicture
  307. Dim ShowingParameters As Boolean
  308. ' *******************************************************
  309. ' Rotate the points in the cube and draw the cube.
  310. ' *******************************************************
  311. Private Sub DrawData(pic As Object)
  312. Dim X As Single
  313. Dim Y As Single
  314. Dim z As Single
  315. Dim old_draw As Integer
  316. Dim old_fill As Integer
  317. Dim t1(1 To 4, 1 To 4) As Single
  318. Dim t2(1 To 4, 1 To 4) As Single
  319. Dim T12(1 To 4, 1 To 4) As Single
  320. Dim T123(1 To 4, 1 To 4) As Single
  321. Dim pt As Point3D
  322.     MousePointer = vbHourglass
  323.     ' Get constants for the surfaces.
  324.     LightKd = CSng(KdText.Text)
  325.     LightKa = CSng(KaText.Text)
  326.     ' Prevent overflow errors when drawing lines
  327.     ' too far out of bounds.
  328.     On Error Resume Next
  329.     ' Cull backfaces.
  330.     ThePicture.Culled = False
  331.     m3SphericalToCartesian EyeR, EyeTheta, EyePhi, X, Y, z
  332.     ThePicture.Cull X, Y, z
  333.     ' Clip faces behind the center of projection.
  334.     ThePicture.ClipEye EyeR
  335.     ' Transform coordinates into pixels.
  336.     m3Scale t1, _
  337.         Pict.ScaleX(1, Pict.ScaleMode, vbPixels), _
  338.         Pict.ScaleY(1, Pict.ScaleMode, vbPixels), _
  339.         1
  340.     m3Translate t2, _
  341.         -Pict.ScaleX(Pict.ScaleLeft, Pict.ScaleMode, vbPixels), _
  342.         -Pict.ScaleY(Pict.ScaleTop, Pict.ScaleMode, vbPixels), _
  343.         0
  344.     m3MatMultiply T12, t1, t2
  345.     m3MatMultiplyFull T123, Projector, T12
  346.     ' Transform the points.
  347.     ThePicture.ApplyFull T123
  348.     ' Clear the screen. We must do this before
  349.     ' selecting the pen and brush since Cls resets
  350.     ' the pen and brush to default values.
  351.     pic.Cls
  352.     ' Prepare to fill polygons.
  353.     old_draw = pic.DrawStyle
  354.     old_fill = pic.FillStyle
  355.     pic.DrawStyle = vbInvisible
  356.     pic.FillStyle = vbFSSolid
  357.     ' Display the data.
  358.     ThePicture.DrawShaded pic, EyeR
  359.     pic.Refresh
  360.     ' Restore the old draw and fill styles.
  361.     pic.DrawStyle = old_draw
  362.     pic.FillStyle = old_fill
  363.     ' Display the viewing parameters.
  364.     ShowViewingParameters
  365.     MousePointer = vbDefault
  366. End Sub
  367. Sub ShowViewingParameters()
  368.     ShowingParameters = True
  369.     RText.Text = Format$(EyeR, "0.0000")
  370.     ThetaText.Text = Format$(EyeTheta, "0.0000")
  371.     PhiText.Text = Format$(EyePhi, "0.0000")
  372.     RText.Refresh
  373.     ThetaText.Refresh
  374.     PhiText.Refresh
  375.     ShowingParameters = False
  376. End Sub
  377. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  378.     Select Case KeyCode
  379.         Case vbKeyLeft
  380.             EyeTheta = EyeTheta - Dtheta
  381.         
  382.         Case vbKeyRight
  383.             EyeTheta = EyeTheta + Dtheta
  384.         
  385.         Case vbKeyUp
  386.             EyePhi = EyePhi - Dphi
  387.         
  388.         Case vbKeyDown
  389.             EyePhi = EyePhi + Dphi
  390.                 
  391.         Case Else
  392.             Exit Sub
  393.     End Select
  394.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  395.     DrawData Pict
  396. End Sub
  397. Private Sub Form_KeyPress(KeyAscii As Integer)
  398.     Select Case KeyAscii
  399.         Case Asc("+")
  400.             EyeR = EyeR + dr
  401.         
  402.         Case Asc("-")
  403.             EyeR = EyeR - dr
  404.         
  405.         Case Else
  406.             Exit Sub
  407.     End Select
  408.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  409.     DrawData Pict
  410. End Sub
  411. Private Sub Form_Load()
  412.     ' Make sure the screen supports palettes.
  413.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  414.         Beep
  415.         MsgBox "This monitor does not support palettes.", _
  416.             vbCritical
  417.         End
  418.     End If
  419.     ' Get system palette size and # static colors.
  420.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  421.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  422.     StaticColor1 = NumStaticColors \ 2 - 1
  423.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  424.     ' Fill the picture's palette with grays.
  425.     MatchGrayPalette Pict
  426.     Pict.Cls
  427.     ' Initialize the eye position.
  428.     EyeR = 20
  429.     EyeTheta = PI * 0.2
  430.     EyePhi = PI * 0.05
  431.     ' Initialize the projection transformation.
  432.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  433. End Sub
  434. ' ***********************************************
  435. ' Load the control's palette so the non-static
  436. ' colors are grays. Map the logical palette to
  437. ' match the system palette. Convert the image to
  438. ' use the non-static grays.
  439. ' Leave new system palette entries in SysPal().
  440. ' ***********************************************
  441. Sub MatchGrayPalette(pic As Control)
  442. Dim origpal(0 To 255) As PALETTEENTRY
  443. Dim wid As Long
  444. Dim hgt As Long
  445. Dim bytes() As Byte
  446. Dim i As Integer
  447. Dim bm As BITMAP
  448. Dim hbm As Integer
  449. Dim status As Long
  450. Dim X As Integer
  451. Dim Y As Integer
  452. Dim gray As Single
  453. Dim dgray As Single
  454. Dim c As Integer
  455. Dim clr As Integer
  456. Dim logpal As Long
  457.     ' Make sure pic has the foreground palette.
  458.     pic.ZOrder
  459.     status = RealizePalette(pic.hdc)
  460.     DoEvents
  461.     ' Get the system palette entries.
  462.     status = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, origpal(0))
  463.         
  464.     ' Get the image pixels.
  465.     hbm = pic.Image
  466.     status = GetObject(hbm, BITMAP_SIZE, bm)
  467.     wid = bm.bmWidthBytes
  468.     hgt = bm.bmHeight
  469.     ReDim bytes(1 To wid, 1 To hgt)
  470.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  471.     ' Make the logical palette as big as possible.
  472.     logpal = pic.Picture.hPal
  473.     If ResizePalette(logpal, SysPalSize) = 0 Then
  474.         Beep
  475.         MsgBox "Error resizing logical palette.", _
  476.             vbExclamation
  477.         Exit Sub
  478.     End If
  479.     ' Blank the non-static colors.
  480.     For i = 0 To StaticColor1
  481.         syspal(i) = origpal(i)
  482.     Next i
  483.     For i = StaticColor1 + 1 To StaticColor2 - 1
  484.         With syspal(i)
  485.             .peRed = 0
  486.             .peGreen = 0
  487.             .peBlue = 0
  488.             .peFlags = PC_NOCOLLAPSE
  489.         End With
  490.     Next i
  491.     For i = StaticColor2 To 255
  492.         syspal(i) = origpal(i)
  493.     Next i
  494.     status = SetPaletteEntries(logpal, 0, SysPalSize, syspal(0))
  495.     ' Insert the non-static grays.
  496.     gray = 0
  497.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  498.     For i = StaticColor1 + 1 To StaticColor2 - 1
  499.         c = gray
  500.         gray = gray + dgray
  501.         With syspal(i)
  502.             .peRed = c
  503.             .peGreen = c
  504.             .peBlue = c
  505.         End With
  506.     Next i
  507.     status = SetPaletteEntries(logpal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, syspal(StaticColor1 + 1))
  508.     ' Realize the gray palette.
  509.     status = RealizePalette(pic.hdc)
  510.     pic.Refresh
  511. End Sub
  512. Private Sub mnuFileExit_Click()
  513.     Unload Me
  514. End Sub
  515. Private Sub mnuFileLoad_Click()
  516. Dim fname As String
  517. Dim filenum As Integer
  518. Dim txt As String
  519. Dim xmin As Single
  520. Dim ymin As Single
  521. Dim xmax As Single
  522. Dim ymax As Single
  523.     ' Allow the user to pick a file.
  524.     On Error Resume Next
  525.     LoadDialog.filename = "*.APF"
  526.     LoadDialog.ShowOpen
  527.     LoadDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  528.     If Err.Number = cdlCancel Then
  529.         Unload LoadDialog
  530.         Exit Sub
  531.     ElseIf Err.Number <> 0 Then
  532.         Unload LoadDialog
  533.         Beep
  534.         MsgBox "Error selecting file.", , vbExclamation
  535.         Exit Sub
  536.     End If
  537.     On Error GoTo 0
  538.     fname = LoadDialog.filename
  539.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  540.         - Len(LoadDialog.FileTitle) - 1)
  541.     ' Clear the picture.
  542.     Set ThePicture = Nothing
  543.     ' Open the file.
  544.     filenum = FreeFile
  545.     Open fname For Input As #filenum
  546.     ' Make sure it's an Object Picture File.
  547.     Input #filenum, txt
  548.     If txt <> "3D APF PICTURE" Then
  549.         Close filenum
  550.         Caption = "Light2"
  551.         Beep
  552.         MsgBox "Error reading file """ & fname & """.", , vbExclamation
  553.         Exit Sub
  554.     End If
  555.     ' Read the picture.
  556.     Set ThePicture = New ObjPicture
  557.     ThePicture.FileInput filenum
  558.     ' Close the file.
  559.     Close filenum
  560.     Caption = "Light2 [" & LoadDialog.FileTitle & "]"
  561.     ' Refresh the display.
  562.     DrawData Pict
  563. End Sub
  564. Private Sub PhiText_Change()
  565.     If ShowingParameters Then Exit Sub
  566.     EyePhi = CSng(PhiText.Text)
  567.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  568.     DrawData Pict
  569. End Sub
  570. Private Sub RText_Change()
  571.     If ShowingParameters Then Exit Sub
  572.     EyeR = CSng(RText.Text)
  573.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  574.     DrawData Pict
  575. End Sub
  576. Private Sub ThetaText_Change()
  577.     If ShowingParameters Then Exit Sub
  578.     EyeTheta = CSng(ThetaText.Text)
  579.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  580.     DrawData Pict
  581. End Sub
  582.